package com.suixinsanren.design.iterator.demo03;

public class DinerMenuIterator implements Iterator<MenuItem> {

    private MenuItem[] menuItems;
    private int position = 0;

    public DinerMenuIterator(DinerMenu dinerMenu) {
        this.menuItems = dinerMenu.getMenuItems();
    }

    @Override
    public boolean hasNext() {
        return position < menuItems.length && menuItems[position] != null;
    }

    @Override
    public MenuItem next() {
        return menuItems[position++];
    }

    @Override
    public void remove() {
        if (position <= 0) {
            throw new IllegalArgumentException("删除失败");
        }
        for (int i = position; i < menuItems.length; i++) {
            menuItems[i - 1] = menuItems[i];
        }
        position--;
    }
}
